package com.sh.suhe.utildemos.getViewPx;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.sh.suhe.utildemos.R;
import com.umeng.commonsdk.debug.D;

import java.nio.ByteBuffer;

public class GetPxColorActivity extends AppCompatActivity {
    private String TAG = GetPxColorActivity.class.getSimpleName();
    MediaProjectionManager mediaProjectionManager;
    MediaProjection mediaProjection;
    int REQUEST_CODE = 100;

    Button but;
    ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_px_color);
        but = findViewById(R.id.but);
        imageView = findViewById(R.id.color_image);

        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.e(TAG, String.format("action %d x:%f y:%f", event.getAction(), event.getX(), event.getY()));
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
//                        event.getX();
                        getPxColor(event.getX(), event.getY() + 160);
                        break;
                }
                return false;
            }
        });
        getMediaProjectionManager();

    }
    public void clickView (View view) {
        getPxColor(300,600);
    }

    public void getMediaProjectionManager() {
        mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        if (mediaProjectionManager != null) {
            startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
           mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
            ImageReader reader = ImageReader.newInstance(displayMetrics.widthPixels, displayMetrics.heightPixels,
                    PixelFormat.RGBA_8888, 1);
            mediaProjection.createVirtualDisplay("screencap", displayMetrics.widthPixels, displayMetrics.heightPixels, displayMetrics.densityDpi,
                    DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, reader.getSurface(), null, null);
            reader.setOnImageAvailableListener(listener, null);

        }
    }
    Bitmap bitmap ;
    private ImageReader.OnImageAvailableListener listener = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            Log.e(TAG, "onImageAvailable");
            Image image = reader.acquireLatestImage();
            if (image != null) {
                int width = image.getWidth();
                int height = image.getHeight();
                Image.Plane[] planes = image.getPlanes();
                ByteBuffer buffer = planes[0].getBuffer();
                int pixelStride = planes[0].getPixelStride();
                int rowStride = planes[0].getRowStride();
                int rowPadding = rowStride - pixelStride * width;
                if (bitmap == null) {
                    bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
                }
                bitmap.copyPixelsFromBuffer(buffer);
                image.close();
            }
        }
    };
    void getPxColor(float x, float y){
        if (bitmap != null) {
           int color = bitmap.getPixel((int)x, (int)y);

            int temp = Color.alpha(color);
            Log.e(TAG, "color: " + color + " temp:" + temp);
           but.setTextColor(color);
        }
    }
}